<?php
/**
 * Created by PhpStrom
 * User: 郭碧云
 * Date: 2021/3/1 16:47
 * Desc:
 */

namespace App\Models\Admin;


use App\Models\BaseModel;

class Setting extends BaseModel
{
    protected $table = "setting";
    protected $casts = ['value'=>'array','created_at' => 'datetime:Y-m-d H:i:s',   'updated_at' => 'datetime:Y-m-d H:i:s',   'deleted_at' => 'datetime:Y-m-d H:i:s'];
    protected $fillable = ["site_id", "name", "value"];
    public $timestamps = false;

    protected $declares = [
        "site_id" => ["site_id", "="],
        "name" => ["name", "like"]
    ];


    //添加/修改配置信息
    public function new_setting($params) {
        //站点是否存在配置，存在更新，不存在，插入 todo 检查公用site_id的可实施性
        $isHas = self::where("site_id", getCurrentSiteId())->where("name",$params["name"])->select("name", "value")->get()->toArray();
        if (!empty($isHas)) {
            //更新
            $result = $this->updateInfo($params);
        } else {
            $result = $this->add($params);
        }
        return $result;
    }

    //插入
    public function add($params) {
        //插入
        $params['site_id'] = getCurrentSiteId();
        return self::create($params);
    }

    //更新数据
    public function updateInfo($params) {
        //默认站点，仅支持修改
        return self::where("site_id", getCurrentSiteId())->where("name", $params["name"])->update(["value" => $params['value']]);
    }
}
